import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';
import 'package:xiaomi_shop/app/modules/checkout/controllers/checkout_controller.dart';
import 'package:xiaomi_shop/app/services/screenAdapter.dart';
import 'package:xiaomi_shop/models/index.dart';

import '../controllers/address_list_controller.dart';

class AddressListView extends GetView<AddressListController> {
  const AddressListView({Key? key}) : super(key: key);

  Widget _addressItem(AddressItemModel model) {
    return InkWell(
      onTap: () {
        if (Get.arguments["page"] == "checkout") {
          // 从结算页面进入的
          CheckoutController checkoutController =
              Get.find<CheckoutController>();
          checkoutController.addressModel.value = model;
          Get.back();
        }
      },
      onLongPress: () {
        // 删除收货地址
        Get.defaultDialog(
          title: "提示",
          middleText: "删除收货地址?",
          textCancel: "取消",
          textConfirm: "确认",
          onCancel: () {},
          onConfirm: () {
            controller.deleteAddress(model.id!);
          },
        );
      },
      child: Container(
        padding: EdgeInsets.all(ScreenAdapter.width(30)),
        decoration: BoxDecoration(
            border: Border(bottom: BorderSide(color: Colors.grey.shade200))),
        child: Row(
          children: [
            Checkbox(
              activeColor: Colors.orange.shade900,
              shape: const CircleBorder(),
              value: model.default_address == 1 ? true : false,
              onChanged: (value) {
                if (model.default_address == 1) {
                  return;
                }
                // 修改默认收货地址
                Get.defaultDialog(
                  title: "提示",
                  middleText: "修改为默认收货地址?",
                  textCancel: "取消",
                  textConfirm: "确认",
                  onCancel: () {},
                  onConfirm: () {
                    controller.changeDefaultAddress(model.id!);
                  },
                );
              },
            ),
            Expanded(
                child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  "${model.address}",
                  style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: ScreenAdapter.fontSize(48)),
                ),
                SizedBox(
                  height: ScreenAdapter.height(20),
                ),
                Text("${model.name} ${model.phone}",
                    style: TextStyle(fontSize: ScreenAdapter.fontSize(40)))
              ],
            )),
            IconButton(
                onPressed: () {
                  Get.toNamed("/address-add", arguments: {
                    "type": 1,
                    "id": model.id,
                    "name": model.name,
                    "phone": model.phone,
                    "address": model.address
                  });
                },
                icon: const Icon(Icons.edit))
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('收货地址'),
        centerTitle: true,
      ),
      backgroundColor: Colors.grey.shade200,
      body: Container(
        margin: EdgeInsets.all(ScreenAdapter.width(30)),
        decoration: BoxDecoration(
            color: Colors.white, borderRadius: BorderRadius.circular(10)),
        child: Obx(() => controller.addressList.value.length == 0
            ? Center(
                child: Text("暂无收货地址"),
              )
            : ListView.builder(
                itemCount: controller.addressList.value.length,
                itemBuilder: (context, index) {
                  AddressItemModel model = controller.addressList.value[index];
                  return _addressItem(model);
                },
              )),
      ),
      bottomNavigationBar: Container(
        height: ScreenUtil().bottomBarHeight + ScreenAdapter.height(180),
        color: Colors.white,
        child: Stack(
          children: [
            Positioned(
                left: 0,
                right: 0,
                top: 0,
                height: ScreenAdapter.height(180),
                child: Container(
                  margin: EdgeInsets.all(ScreenAdapter.width(30)),
                  color: Colors.orange.shade900,
                  child: TextButton(
                      onPressed: () {
                        Get.toNamed("/address-add");
                      },
                      child: Text(
                        "新建收货地址",
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: ScreenAdapter.fontSize(44),
                            fontWeight: FontWeight.w800),
                      )),
                ))
          ],
        ),
      ),
    );
  }
}
